<?php

namespace Api\Controller;

class CommentController extends ApiController {

    private $token;
    private $deviceid;
    private $user_log;
    private $user_id;
    private $error_comment;
    private $post_id;
    private $post;
    private $icontype;
    private $Post;
    private $userinfo;

    function __construct() {
        parent::__construct();
        $this->token = I('request.token');
        $this->deviceid = I('request.deviceid');
        $this->userinfo = $this->verify_token($this->token, $this->deviceid);
        $this->user_id = $this->userinfo['user_id'];

        $this->error_comment = L('error_comment');
        $this->post_id = I('request.postid');
        $this->Post = D('Post');
        $this->post = $this->Post->getPostbypid($this->post_id);
        if ($this->post['is_comment'] == 1) {
            $this->error($this->error_comment['nocomment']);
        }
        $this->icontypes = array(1, 2, 3, 4);
    }

    function addicon() {
        $icontype = I('request.icon');
        if (!in_array($icontype, $this->icontypes)) {
            $this->error($this->error_comment['noicon']);
        }
        $Member = D('Member');
        $member = D('Member')->getMembyids($this->user_id, $this->post['class_id']);
        if (!$member) {
            $this->error($this->error_comment['noinclass']);
        }
        if ($member['power'] <= 0) {
            $this->error($this->error_comment['nopower']);
        }
        //添加记录
        $Reply = D('Reply');
        $comment = $Reply->getCommentbyIds($this->post['id'], $this->user_id);
        if ($comment['id']) {
            $this->error($this->error_comment['moreicons']);
        } else {
            $Reply->addComment($this->post['id'], $icontype, $this->user_id);
        }
        //添加统计记录
        $icons = array('icon_1' => 0, 'icon_2' => 0, 'icon_3' => 0, 'icon_4' => 0);
        $post_icons = $Reply->getIcons($this->post['id']);
        if (!$post_icons['post_id'] || empty($post_icons['post_id'])) {
            $Reply->addIcons($this->post['id'], $icons);
            $icons['icon_' . $icontype] = $icons['icon_' . $icontype] + 1;
            $upicons = $icons;
        } else {
            foreach ($icons as $key => $value) {
                $upicons[$key] = $post_icons[$key];
            }
            $upicons['icon_' . $icontype] = $upicons['icon_' . $icontype] + 1;
        }
        if ($Reply->upIcons($this->post['id'], $upicons)) {
            $arrInfo = array();
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error[$this->error_comment['oserror']];
        }
    }

    function geticonlist() {
        $start = I('request.start') != FALSE ? I('request.start') : 0;
        $Member = D('Member');
        $member = D('Member')->getMembyids($this->user_id, $this->post['class_id']);
        if (!$member) {
            $this->error($this->error_comment['noinclass']);
        }
        if ($this->post['is_hide'] && ($this->post['show_id'] > $member['power'])) {
            $this->error($this->error_comment['nopower']);
        }

        $members = D('Member')->getCmembersbyCid($this->post['class_id'], $start);
        $Mname = array();
        foreach ($members as $value) {
            $Mname[] = array('name' => $value['rename'], 'uid' => $value['user_id']);
        }
        $list = array();
        $Reply = D('Reply');
        $list = $Reply->getList($this->post_id,$this->post['class_id'] ,$start);
        $newlist = array();
        foreach ($list as $key => $value) {
            $newlist['i'.$value['icon']][] = array('name' => $value['name'], 'uid' => $value['uid']);
        }
        foreach ($Mname as $value) {
            if ((!in_array($value, $newlist['i1'])) && (!in_array($value, $newlist['i2'])) && (!in_array($value, $newlist['i3'])) && (!in_array($value, $newlist['i4']))) {
                $newlist['i0'][] = $value;
            }
        }
        $this->success(__FUNCTION__, $newlist);
    }

}
